package com.sicau.smart_farmer.service;

import com.sicau.smart_farmer.entity.User;
import com.sicau.smart_farmer.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class UserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserRepository userRepository;


    /**
     * 添加用户
     * @param user
     * @return
     */
    public boolean addUser(User user){
        User save = userRepository.save(user);
        return save != null;
    }

    /**
     *根据用户id删除用户
     * @param id user的Id
     */
    public void deleteUserById(String id){
        userRepository.deleteById(id);
    }

    /**
     * 根据id去更改用户信息。
     * @param user
     * @return
     */
    public User updateUser(User user) {

        if (!userRepository.findById(user.getId()).isPresent()){
            return null;
        }

        return userRepository.save(user);
    }

    /**
     * 分页查询所有用户
     *
     * @param page    页面
     * @param pageNum 每一页的数量
     * @return
     */
    public List<User> selectAllUserByPage(Integer page, Integer pageNum) {
        List<User> users = mongoTemplate.find(new Query().with(PageRequest.of(page - 1, pageNum)), User.class);
        return users;
    }

    /**
     * 查询所有iser
     * @return
     */
    public List<User> selectAllUser() {
        List<User> users = mongoTemplate.findAll(User.class);
        return users;
    }


    /**
     * 根据用户名和身份证号模糊查询
     *
     * @param name
     * @param idCardNum
     * @return
     */
    public List<User> searchUserLike(String name, String idCardNum) {
        List<User> users = null;
        if(StringUtils.isEmpty(name) && !StringUtils.isEmpty(idCardNum)){
            users = userRepository.findUsersByIdCardNumLike(idCardNum);
        }else if(!StringUtils.isEmpty(name) && StringUtils.isEmpty(idCardNum)){
            users = userRepository.findUsersByNameLike(name);
        }else if(!StringUtils.isEmpty(name) && !StringUtils.isEmpty(idCardNum)){
            users = userRepository.findUsersByNameLikeAndIdCardNumLike(name, idCardNum);
        }

        return users;
    }

    /**
     * 根据用户id查询用户
     * @param userId
     * @return
     */
    public User selectUserById(String userId){
        Optional<User> byId = userRepository.findById(userId);
        return byId.get();
    }

    /**
     * 批量更新用户是否市新型职业农民的状态
     * @param userIdList
     * @param identification
     */
    public void updateUserIdentification(List<String> userIdList, boolean identification) {
        for (String item : userIdList) {
            Optional<User> byId = userRepository.findById(item);
            //如果用户不存在则continue
            if (!byId.isPresent()){
                continue;
            }
            User user = byId.get();
            user.setIdentification(identification);
            userRepository.save(user);
        }
    }



    /**
     * 查询不在批次中的用户信息
     * @param ids
     * @return
     */
    public List<User> queryUsersByIdNotIn(List<String> ids){
        List<User> usersByIdNotIn = userRepository.findUsersByIdNotIn(ids);
        return usersByIdNotIn;
    }

    /**
     * 查询所有新型农民
     * @return
     */
    public List<User> queryAllIdentifiedUser(boolean isIdentified){
        List<User> usersByIdentification = userRepository.findUsersByIdentification(isIdentified);
        return usersByIdentification;
    }




}
